package com.zhicheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhicheng.mapper.CategoryMapper;
import com.zhicheng.pojo.Category;
import com.zhicheng.pojo.CategoryTwo;
import com.zhicheng.pojo.dto.CategoryPageQueryDTO;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.vo.CategoryTwoVO;
import com.zhicheng.pojo.vo.CategoryVO;
import com.zhicheng.pojo.vo.GoodVO;
import com.zhicheng.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据id查询一级分类
     * @param id
     * @return
     */
    @Override
    public CategoryVO getCategoryById(Long id) {
        // 根据一级分类id查询一级分类数据
        CategoryVO categoryVO = categoryMapper.selectCategoryById(id);
        // 根据二级分类id查询二级分类数据
        List<CategoryVO> categoryTwoVo = categoryMapper.selectCategoryTwoByCategoryId(id);
        // 根据商品所属分类id查询二级分类下的商品
        for (CategoryVO categoryTwoV0 : categoryTwoVo) {
            List<GoodVO> goods = categoryMapper.selectGoodByCategoryId(categoryTwoV0.getId());
            categoryTwoV0.setGoods(goods);
        }
        // 根据商品父分类id查询商品
        List<GoodVO> goods = categoryMapper.selectGoodByParentId(id);

        //将查询到的数据封装到返回对象
        categoryVO.setChildren(categoryTwoVo);
        categoryVO.setGoods(goods);
        return categoryVO;
    }

    @Override
    public CategoryTwoVO getGoodBySubCategoryId(Long id) {
        CategoryTwoVO SubCategory = categoryMapper.selectCategoryTwoById(id);
        // 根据商品所属分类id查询二级分类下的商品
        List<GoodVO> goods = categoryMapper.selectGoodByCategoryId(id);
        SubCategory.setGoods(goods);

        return SubCategory;
    }

    @Override
    public PageResult<GoodVO> list(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.创建PageResult对象
        PageResult<GoodVO> pageResult =new PageResult<>();
        //2.开启分页查询PageHelper
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Long categoryId = categoryPageQueryDTO.getCategoryId();
        List<GoodVO> goodVOS = categoryMapper.selectGoodByCategoryId(categoryId);
        //Page中提供了方法，可以获取PageHelper分页查询后得到的总记录数和当前页数据
        Page<GoodVO> p = (Page<GoodVO>) goodVOS;
        //把数据填充到PageResult对象中
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public PageResult<GoodVO> getByGoodName(String name, int page, int pageSize) {
        //1.创建PageResult对象
        PageResult<GoodVO> pageResult =new PageResult<>();
        //2.开启分页查询PageHelper
        PageHelper.startPage(page, pageSize);
        List<GoodVO> goodList = categoryMapper.getByGoodName(name);
        //Page中提供了方法，可以获取PageHelper分页查询后得到的总记录数和当前页数据
        Page<GoodVO> p = (Page<GoodVO>) goodList;
        //把数据填充到PageResult对象中
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public List<CategoryTwo> getSubCategory() {
        List<CategoryTwo> categoryTwos = categoryMapper.getSubCategory();
        return categoryTwos;
    }

    @Override
    public PageResult<Category> getCategoryPage(int page, int pageSize) {
        PageResult<Category> pageResult = new PageResult<>();
        PageHelper.startPage(page,pageSize);
        List<Category> categories = categoryMapper.getCategoryPage();
        Page<Category> p = (Page<Category>) categories;
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public void addCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateCategory(category);
    }

    @Override
    public void deleteCategory(Long id) {
        categoryMapper.deleteCategory(id);
    }

    @Override
    public PageResult<CategoryTwo> getSubCategoryPage(Long categoryId,int page, int pageSize) {
        PageResult<CategoryTwo> pageResult = new PageResult<>();
        PageHelper.startPage(page,pageSize);
        List<CategoryTwo> subCategories = categoryMapper.getSubCategoryQuery(categoryId);
        Page<CategoryTwo> p = (Page<CategoryTwo>) subCategories;
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public void addSubCategory(CategoryTwo categoryTwo) {
        categoryTwo.setCreateTime(LocalDateTime.now());
        categoryTwo.setUpdateTime(LocalDateTime.now());
        CategoryVO category = categoryMapper.selectCategoryById(categoryTwo.getCategoryId());
        categoryTwo.setParentName(category.getName());
        categoryMapper.addSubCategory(categoryTwo);
    }

    @Override
    public void updateSubCategory(CategoryTwo categoryTwo) {
        categoryTwo.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateSubCategory(categoryTwo);
    }

    @Override
    public void deleteSubCategory(Long id) {
        categoryMapper.deleteSubCategory(id);
    }

    @Override
    public List<Category> getCategory() {
        return categoryMapper.getCategoryPage();
    }
}
